package com.intel.factorytest.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import com.intel.factorytest.R;

public class Accelerometer extends BaseActivity implements SensorEventListener{
    
    private SensorManager mSm = null;
    private Sensor mAccelerometerSenesor = null;
    private TextView mTextView = null;
    private static final String TEST_ITEM = "test_item";
    private float gx1 = 0;
    private float gy1 = 1;
    private float gz1 = 2;
    private float gx = 0;
    private float gy = 1;
    private float gz = 2;
    private int count = 0;
    private final String TAG  = "Accelerometer";
    private String message = null; 
    private static final String TEST_RESULT = "test_result";
    private boolean is_pass;
    private static final String TEST_ITEM_RESULT = "test_item_result";
    
    public void onCreate(Bundle b) {
        super.onCreate(b);    

        mTextView = new TextView(this);
        mTextView.setTextSize(25.0f);
        mTextView.setGravity(Gravity.CENTER);
        mSm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mAccelerometerSenesor = mSm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if(mAccelerometerSenesor == null){
            mTextView.setText(R.string.SERVICEMENU_ACCELEROMETER_GENSER_NULL_TXT);
        }
        setContentView(mTextView);
    }
    public void onResume(){
        super.onResume();
        mSm.registerListener(this, mAccelerometerSenesor, SensorManager.SENSOR_DELAY_GAME);
    }
    public void onPause(){
        super.onPause();
        mSm.unregisterListener(this);
    }
     /*public boolean onTouchEvent(MotionEvent event)
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN )
            {
                showDialog(0);            
            }
            return true;
        }*/
        
        @Override
        protected Dialog onCreateDialog(int none)
        {        
            return new AlertDialog.Builder(this)
//            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(R.string.SERVICEMENU_DIALOG_TITLE_TXT)
            .setPositiveButton(R.string.SERVICEMENU_DIALOG_YES_TXT, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked OK so do some stuff */
                    finish();
                }
            })
            .setNegativeButton(R.string.SERVICEMENU_DIALOG_NO_TXT, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Cancel so do some stuff */
                    finish();
                }
            })
            .create();
        }
        
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
                
        }
         
        public void onSensorChanged(SensorEvent event) {
            long currentTimeMillis = System.currentTimeMillis();
            if (count == 0) {
                gx = event.values[0];
                gy = event.values[1];
                gz = event.values[2];
                message    = " X:"+gx+"\n Y:"+gy+"\n Z:"+gz;
            }else {
                gx1 = event.values[0];
                gy1 = event.values[1];
                gz1 = event.values[2];
                message    = " X:"+gx1+"\n Y:"+gy1+"\n Z:"+gz1;
            }
            count++;
            //if(count >= 40) {
            if(count >= 10) {
                if (gx!=gx1 || gy!=gy1 || gz!=gz1) {
                    is_pass = true;
                    message = message + "\n" + getString(R.string.SERVICEMENU_GSENSOR_IS_SUCCESS);
                    System.out.println(message);
                    TestResult(message);
                }else {
                    is_pass = false;
                }
            }
            mTextView.setText(message);
            android.util.Log.i("meilianggang","spend time is " + (System.currentTimeMillis() - currentTimeMillis) + "  currentTimeMillis = " + currentTimeMillis);

        }
        
    @Override
    public void finish() {
        tc.setTestResult(is_pass);
        tc.setResultInfo(message);
        super.finish();
    }
    public boolean ac_ispass() {
        return is_pass;
    } 

    private void TestResult(String result) {
        String message = "";

        File mFile = new File("/mnt/sdcard/gsensor.txt");
        //判断文件是否存在
        if (mFile.exists()) {
            //mFile.delete();
            return;
        }
        
        message = result + " \n" +" G-sensor is pass ";
        try {
            //创建文件
            mFile.createNewFile();
            RandomAccessFile raf = new RandomAccessFile(mFile, "rwd");
            raf.seek(mFile.length());
            raf.write(message.getBytes());
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

